package cn.zhj.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.zhj.user.domain.User;
import cn.zhj.user.mapper.UserMapper;
import cn.zhj.user.query.UserQuery;
import cn.zhj.user.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public User findOne(Long id) {
        return userMapper.findOne(id);
    }

    @Transactional
    @Override
    public void add(User user) {
        //生成随机的颜值
        String salt = RandomUtil.randomString(32);
        //将密码进行加密加盐
        String md5Pwd = SecureUtil.md5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        userMapper.add(user);
    }
    @Transactional
    @Override
    public void delete(Long id) {
        userMapper.delete(id);
    }
    @Transactional
    @Override
    public void update(User user) {
        userMapper.update(user);
    }
    @Transactional
    @Override
    public void patchDel(Long[] ids) {
        userMapper.patchDel(ids);
    }

    @Override
    public PageInfo<User> queryPage(UserQuery userQuery) {
        // 开启分页
        PageHelper.startPage(userQuery.getCurrentPage(),userQuery.getPageSize());
        //
        List<User> list = userMapper.queryPage(userQuery);
        PageInfo<User> PageInfo = new PageInfo<User>(list);
        return PageInfo;
    }
}
